 //
//  CollectViewController.m
//  WHISPER
//
//  Created by lanou3g on 15/12/30.
//  Copyright © 2015年 王艳苹. All rights reserved.
//

#import "CollectViewController.h"
#import "YSAVObject.h"
#import "CollectModel.h"
#import "YPWebViewVC.h"
@interface CollectViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *table;
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,assign)UITableViewCellEditingStyle style;

@end

@implementation CollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataArr = @[].mutableCopy;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我的收藏";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(leftAction)];
    self.table = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.table.separatorStyle = NO;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:_table];
    self.dataArr = [[YSAVObject shareAVObject] queryArray:[[NSUserDefaults standardUserDefaults]objectForKey:@"userName"]];
    self.style = UITableViewCellEditingStyleDelete;
}

//哪些可以被编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

//返回编辑样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
   return self.style;
}

//完成编辑
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.style == UITableViewCellEditingStyleDelete) {
        CollectModel *model = self.dataArr[indexPath.row];
        [self.dataArr removeObjectAtIndex:indexPath.row];
        [[YSAVObject shareAVObject]deleteForAvObjectWithTitle:model.title Url:model.url userName:[[NSUserDefaults standardUserDefaults]objectForKey:@"userName"]];
        [self.table deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *str = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    
    CollectModel *model = self.dataArr[indexPath.row];
    cell.textLabel.text = model.title;
    cell.backgroundColor = [UIColor colorWithRed:arc4random()% 256 /255.0 green:arc4random()% 256 /255.0 blue:arc4random()% 256 /255.0 alpha:arc4random()% 256 /255.0];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    YPWebViewVC *web = [[YPWebViewVC alloc]init];
    CollectModel *model = self.dataArr[indexPath.row];
    web.tingID = model.url;
    web.shareTitle = model.title;
    web.type = self.navigationItem.title;
    [self.navigationController presentViewController:[[UINavigationController alloc]initWithRootViewController:web] animated:YES completion:nil];
}

-(void)leftAction{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
